<template>
  <common-layout>
    <div class="login">
      <div class="top">
        <div class="header">
          <!-- <img alt="logo" class="logo" :src="icons" /> -->
          <img class="logo" :src="require('../../assets/img/logo.png')" />
          <span class="titles">远媒数据</span>
          <!-- <span class="title">{{ systemName }}</span> -->
        </div>
        <div class="desc">助力全球开发者变现</div>
      </div>
      <div class="login">
        <a-form @submit="onSubmit" :form="form">
          <a-alert type="error" :closable="true" v-show="error" :message="error" showIcon style="margin-bottom: 24px" />
          <a-form-item>
            <a-input
              autocomplete="autocomplete"
              size="large"
              placeholder="用户名"
              v-decorator="['username', { rules: [{ required: true, message: '请输入账户名', whitespace: true }] }]"
            >
              <a-icon slot="prefix" type="user" />
            </a-input>
          </a-form-item>
          <a-form-item>
            <a-input
              size="large"
              placeholder="密码"
              autocomplete="autocomplete"
              type="password"
              v-decorator="['password', { rules: [{ required: true, message: '请输入密码', whitespace: true }] }]"
            >
              <a-icon slot="prefix" type="lock" />
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-button :loading="logging" style="width: 100%" size="large" htmlType="submit" type="primary">登录</a-button>
          </a-form-item>
        </a-form>
      </div>
      <div class="footer">
        <span>© 2021 远媒数据</span>
        <a href="https://beian.miit.gov.cn" target="_blank" style="cursor: pointer">©浙ICP备2021018293号-1</a>
      </div>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from '_c/CommonLayout'
import { BaseIcon } from './index'
import { toRoute } from '@/lib/utils'
import { mapMutations } from 'vuex'
import { userLogin } from '@/api/api'
export default {
  name: 'Login',
  components: { CommonLayout },
  data() {
    return {
      systemName: '趣味',
      logging: false,
      error: '',
      form: this.$form.createForm(this),
      icons: '',
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.icons = BaseIcon
    })
  },
  methods: {
    ...mapMutations(['changeLogin']),
    onSubmit(e) {
      let _this = this
      e.preventDefault()
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true
          const [username, password] = [this.form.getFieldValue('username'), this.form.getFieldValue('password')]
          userLogin({ username, password })
            .then((res) => {
              const { token, userType } = res.data.data
              if (token) {
                localStorage.setItem('userName', username)
                localStorage.setItem('userType', userType)
                /**
                 *@token 将用户token保存到vuex中
                 **/
                _this.changeLogin({
                  Authorization: token,
                })
                toRoute(this, '/')
                this.$msgSuc('登录成功')
              }
              this.logging = false
            })
            .catch(() => {
              this.logging = false
              this.$msgErr('用户名或密码错误，请重试')
            })
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.logo {
  width: 50px;
}
.titles {
  font-size: 24px;
  font-weight: bold;
}
.footer {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: gray;
  margin-top: 20rem;
  a {
    color: gray;
  }
}
.top {
  text-align: center;
  .header {
    height: 44px;
    line-height: 44px;
    a {
      text-decoration: none;
    }
    .logo {
      height: 44px;
      vertical-align: top;
      margin-right: 5px;
    }
    .title {
      font-size: 33px;
      color: rgba(0, 0, 0, 0.85);
      font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
      font-weight: 600;
      position: relative;
      top: 2px;
    }
  }
  .desc {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.45);
    margin-top: 12px;
    margin-bottom: 40px;
  }
}
.login {
  width: 368px;
  margin: 0 auto;
  @media screen and (max-width: 576px) {
    width: 95%;
  }
  @media screen and (max-width: 320px) {
    .captcha-button {
      font-size: 14px;
    }
  }
  .icon {
    font-size: 24px;
    color: red;
    margin-left: 16px;
    vertical-align: middle;
    cursor: pointer;
    transition: color 0.3s;

    &:hover {
      color: red;
    }
  }
}
</style>
